import copy

from django.utils.safestring import mark_safe


class Pagernation(object, ):
    """ 分页"""

    def __init__(self, request, query_set, per_page_count=10):

        self.query_dict = copy.deepcopy(request.GET)
        self.query_dict._mutable = True
        self.query_set = query_set
        total_count = query_set.count()
        self.total_count = total_count
        # 计算出总共多少页面
        self.total_page, div = divmod(total_count, per_page_count)
        if div:
            self.total_page += 1
        page = request.GET.get('page')
        if not page:
            page = 1
        else:
            if not page.isdecimal():
                page = 1
            else:
                page = int(page)
                if page <= 0:
                    page = 1

        self.page = page
        self.per_page_count = per_page_count

        self.start = (page - 1) * per_page_count
        self.end = page * per_page_count
        self.total_count = total_count

    def html(self):
        pager_list = []
        if not self.total_page:
            return ""
        if self.total_page <= 11:
            start_page = 1
            end_page = self.total_page
        else:
            if self.page < 6:
                start_page = 1
                end_page = 11
            else:
                if (self.page + 5) > self.total_page:
                    start_page = self.total_page - 10
                    end_page = self.total_page
                else:
                    start_page = self.page - 5
                    end_page = self.page + 5
        self.query_dict.setlist('page', [1])
        pager_list.append('<li><a href="?{}">首页</a></li>'.format(self.query_dict.urlencode()))
        if self.page > 1:
            self.query_dict.setlist('page', [self.page - 1])
            pager_list.append('<li><a href="?{}">上一页</a></li>'.format(self.query_dict.urlencode()))
        for i in range(start_page, end_page + 1):
            self.query_dict.setlist('page', [i])
            if i == self.page:
                item = '<li class="active"><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            else:
                item = '<li><a href="?{}">{}</a></li>'.format(self.query_dict.urlencode(), i)
            pager_list.append(item)
        if self.page < self.total_page:
            self.query_dict.setlist('page', [self.page + 1])
            pager_list.append('<li><a href="?{}">下一页</a></li>'.format(self.query_dict.urlencode()))
        self.query_dict.setlist('page', [self.total_page])
        pager_list.append('<li><a href="?{}">尾页</a></li>'.format(self.query_dict.urlencode()))
        pager_list.append('<li class="disabled"><a>共{}页总计{}条记录</a></li>'.format(self.total_page, self.total_count))
        pager_string = mark_safe("".join(pager_list))

        return pager_string

    def queryset(self):
        return self.query_set[self.start:self.end]
